package com.example.provider.service

import com.example.common.entity.CustomerDubbo
import com.example.common.exception.DemoException
import com.example.common.service.CustomerDubboService
import com.example.provider.db.mysql.model.Customer
import com.example.provider.db.mysql.model.CustomersLog
import com.example.provider.db.mysql.repository.CustomerLogsRepository
import com.example.provider.db.mysql.repository.CustomerRepository
import org.apache.dubbo.config.annotation.DubboService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.transaction.annotation.Transactional
import java.util.*

@DubboService
open class CustomerServiceImpl : CustomerDubboService {

    @Autowired
    lateinit var customerRepository: CustomerRepository
    @Autowired
    lateinit var customerLogsRepository: CustomerLogsRepository


    override fun getByEmail(email: String?): CustomerDubbo {
        val c =  customerRepository.getByEmail(email)
        if (c.isPresent) {
            return CustomerDubbo(c.get().id, c.get().name, c.get().email)
        }
        return CustomerDubbo( 0, null, null)
    }

    override fun getAllCustomers(queryParams: CustomerDubbo): List<CustomerDubbo> {
        customerRepository.findCustomers(queryParams.name, queryParams.email)
        return customerRepository.findCustomers(queryParams.name, queryParams.email).map {
            CustomerDubbo(it.id, it.name, it.email)
        }.toList()
    }

    override fun findCustomerById(id: Long): CustomerDubbo {
       val c = customerRepository.get(id)
        if (c.isPresent){
            return CustomerDubbo(c.get().id, c.get().name, c.get().email)
        }
        return CustomerDubbo(0,null,null)
    }

    @Transactional("transactionManager")
    override fun deleteCustomer(id: Long) {
        val existingCustomer = customerRepository.get(id)
        if (existingCustomer.isPresent) {
            customerLogsRepository.save(CustomersLog(null, "delete customer:$id"))
            customerRepository.deleteById(id)
        } else {
            throw DemoException("Customer not exist")
        }

    }
    @Transactional("transactionManager")
    override fun createCustomer(customerDubbo: CustomerDubbo) {
        val c= getByEmail(customerDubbo.email)
        if (c.id!! >0){
            throw DemoException("Customer email exist!")
        }
        val customer = Customer(null, customerDubbo.name, customerDubbo.email)
        customerRepository.save(customer)
        customerLogsRepository.save(CustomersLog(null, "createCustomer:${customer}"))

    }
    @Transactional("transactionManager")
    override fun updateCustomer(customerDubbo: CustomerDubbo) {
        val existingCustomer = customerRepository.get(customerDubbo.id!!)
        if (existingCustomer.isPresent) {
            val c= getByEmail(customerDubbo.email)
            if (c.id!! >0){
                throw DemoException("Customer email exist!")
            }
            customerRepository.save(Customer(customerDubbo.id, customerDubbo.name, customerDubbo.email))
            customerLogsRepository.save(CustomersLog(null, "updateCustomer:${existingCustomer.toString()} to ${customerDubbo.toString()}"))
        } else {
            throw DemoException("Customer not exist")
        }
    }


}